package com.example.apitesttool.agent;

import java.util.*;

/**
 * 智能体状态类
 * 存储智能体在整个对话和任务执行过程中的上下文信息
 */
public class AgentState {
    
    // 用户输入和对话历史
    private String userQuery;
    private List<String> conversationHistory;
    
    // 意图识别和任务状态
    private String currentIntent;
    private String taskStatus; // "PENDING", "IN_PROGRESS", "COMPLETED", "ERROR"
    
    // API 接口信息
    private ApiInfo identifiedApiInfo;
    private List<ApiInfo> candidateApis;
    
    // 参数收集
    private Map<String, Object> collectedParameters;
    private List<String> missingParameters;
    
    // 工具执行结果
    private Object lastToolResult;
    private String errorMessage;
    
    // 流程控制
    private String nextAction;
    private int stepCount;
    private boolean shouldContinue;
    
    public AgentState() {
        this.conversationHistory = new ArrayList<>();
        this.collectedParameters = new HashMap<>();
        this.missingParameters = new ArrayList<>();
        this.candidateApis = new ArrayList<>();
        this.stepCount = 0;
        this.shouldContinue = true;
        this.taskStatus = "PENDING";
    }
    
    /**
     * API 信息数据结构
     */
    public static class ApiInfo {
        private String path;
        private String method;
        private String summary;
        private String description;
        private String serverUrl;
        private Map<String, ParameterInfo> parameters;
        private Map<String, Object> metadata;
        
        public ApiInfo() {
            this.parameters = new HashMap<>();
            this.metadata = new HashMap<>();
        }
        
        // Getters and Setters
        public String getPath() { return path; }
        public void setPath(String path) { this.path = path; }
        
        public String getMethod() { return method; }
        public void setMethod(String method) { this.method = method; }
        
        public String getSummary() { return summary; }
        public void setSummary(String summary) { this.summary = summary; }
        
        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }
        
        public String getServerUrl() { return serverUrl; }
        public void setServerUrl(String serverUrl) { this.serverUrl = serverUrl; }
        
        public Map<String, ParameterInfo> getParameters() { return parameters; }
        public void setParameters(Map<String, ParameterInfo> parameters) { this.parameters = parameters; }
        
        public Map<String, Object> getMetadata() { return metadata; }
        public void setMetadata(Map<String, Object> metadata) { this.metadata = metadata; }
        
        @Override
        public String toString() {
            return String.format("%s %s - %s", method, path, summary);
        }
    }
    
    /**
     * 参数信息数据结构
     */
    public static class ParameterInfo {
        private String name;
        private String type;
        private String location; // "header", "query", "path", "body"
        private boolean required;
        private String description;
        private Object defaultValue;
        
        public ParameterInfo() {}
        
        public ParameterInfo(String name, String type, String location, boolean required) {
            this.name = name;
            this.type = type;
            this.location = location;
            this.required = required;
        }
        
        // Getters and Setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        
        public String getType() { return type; }
        public void setType(String type) { this.type = type; }
        
        public String getLocation() { return location; }
        public void setLocation(String location) { this.location = location; }
        
        public boolean isRequired() { return required; }
        public void setRequired(boolean required) { this.required = required; }
        
        public String getDescription() { return description; }
        public void setDescription(String description) { this.description = description; }
        
        public Object getDefaultValue() { return defaultValue; }
        public void setDefaultValue(Object defaultValue) { this.defaultValue = defaultValue; }
        
        @Override
        public String toString() {
            return String.format("%s (%s, %s, %s)", name, type, location, required ? "必填" : "可选");
        }
    }
    
    // 便利方法
    public void addToHistory(String message) {
        this.conversationHistory.add(message);
    }
    
    public void setParameter(String name, Object value) {
        this.collectedParameters.put(name, value);
        this.missingParameters.remove(name);
    }
    
    public void addMissingParameter(String paramName) {
        if (!this.missingParameters.contains(paramName)) {
            this.missingParameters.add(paramName);
        }
    }
    
    public void incrementStep() {
        this.stepCount++;
    }
    
    public boolean hasError() {
        return this.errorMessage != null && !this.errorMessage.isEmpty();
    }
    
    public void clearError() {
        this.errorMessage = null;
    }
    
    public void setError(String error) {
        this.errorMessage = error;
        this.taskStatus = "ERROR";
    }
    
    public void markCompleted() {
        this.taskStatus = "COMPLETED";
        this.shouldContinue = false;
    }
    
    public void markInProgress() {
        this.taskStatus = "IN_PROGRESS";
    }
    
    // Getters and Setters
    public String getUserQuery() { return userQuery; }
    public void setUserQuery(String userQuery) { this.userQuery = userQuery; }
    
    public List<String> getConversationHistory() { return conversationHistory; }
    public void setConversationHistory(List<String> conversationHistory) { this.conversationHistory = conversationHistory; }
    
    public String getCurrentIntent() { return currentIntent; }
    public void setCurrentIntent(String currentIntent) { this.currentIntent = currentIntent; }
    
    public String getTaskStatus() { return taskStatus; }
    public void setTaskStatus(String taskStatus) { this.taskStatus = taskStatus; }
    
    public ApiInfo getIdentifiedApiInfo() { return identifiedApiInfo; }
    public void setIdentifiedApiInfo(ApiInfo identifiedApiInfo) { this.identifiedApiInfo = identifiedApiInfo; }
    
    public List<ApiInfo> getCandidateApis() { return candidateApis; }
    public void setCandidateApis(List<ApiInfo> candidateApis) { this.candidateApis = candidateApis; }
    
    public Map<String, Object> getCollectedParameters() { return collectedParameters; }
    public void setCollectedParameters(Map<String, Object> collectedParameters) { this.collectedParameters = collectedParameters; }
    
    public List<String> getMissingParameters() { return missingParameters; }
    public void setMissingParameters(List<String> missingParameters) { this.missingParameters = missingParameters; }
    
    public Object getLastToolResult() { return lastToolResult; }
    public void setLastToolResult(Object lastToolResult) { this.lastToolResult = lastToolResult; }
    
    public String getErrorMessage() { return errorMessage; }
    public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; }
    
    public String getNextAction() { return nextAction; }
    public void setNextAction(String nextAction) { this.nextAction = nextAction; }
    
    public int getStepCount() { return stepCount; }
    public void setStepCount(int stepCount) { this.stepCount = stepCount; }
    
    public boolean isShouldContinue() { return shouldContinue; }
    public void setShouldContinue(boolean shouldContinue) { this.shouldContinue = shouldContinue; }
}

